package curator;

import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.junit.Test;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author liyanhao
 * @Date 2023-02-17 21:04
 */
@Slf4j
public class CuratorBaseOperations extends CuratorClusterBase {
    //递归创建子节点
    @Test
    public void testCreateWithParent() throws Exception {
        CuratorFramework curatorFramework = getCuratorFramework();
        String pathWithParent = "/node-parent/sub-node-1";
        String path = curatorFramework.create()
                .creatingParentsIfNeeded()
                .forPath(pathWithParent);
        log.info("curator create node:{} successfully.", path);
    }

    @Test
    public void testCreate() throws Exception {
        CuratorFramework curatorFramework = getCuratorFramework();
        String forPath = curatorFramework
                .create()
                .creatingParentsIfNeeded()
                // protection 模式，防止由于异常原因，导致僵尸节点
                .withProtection()
                .withMode(CreateMode.EPHEMERAL_SEQUENTIAL).
                forPath("/curator-node-1", "some-data".getBytes());
        log.info("curator create node :{}  successfully.", forPath);
    }

    @Test
    public void testGetData() throws Exception {
        CuratorFramework curatorFramework = getCuratorFramework();

        byte[] bytes = curatorFramework.getData().forPath("/curator-node-1");
        log.info("get data from  node :{}  successfully.", new String(bytes));
    }

    @Test
    public void testListChildren() throws Exception {
        CuratorFramework curatorFramework = getCuratorFramework();
        String pathWithParent = "/node-parent";
        List<String> strings = curatorFramework.getChildren().forPath(pathWithParent);
        strings.forEach(System.out::println);
    }

    @Test
    public void testDelete() throws Exception {
        CuratorFramework curatorFramework = getCuratorFramework();
        String pathWithParent = "/node-parent";
        curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath(pathWithParent);
    }

    @Test
    public void testThreadPool()throws Exception{
        CuratorFramework curatorFramework = getCuratorFramework();
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        String zk_node = "/zk-node";
        curatorFramework.getData().inBackground((child, event) -> {
            log.info("background:{}", event);
        }, executorService).forPath(zk_node);
    }
}
